package com.school.studentmanager.controller;

import com.school.studentmanager.model.StudentEntity;
import com.school.studentmanager.respository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/6/14.
 */

@Controller
public class IndexController {

    @Autowired
    StudentRepository studentRepository;


    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    /**
     * 获取学生列表
     *
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/admin/users", method = RequestMethod.GET)
    public String getUsers(ModelMap modelMap) {
        // 查询user表中所有记录
        List<StudentEntity> userList = studentRepository.findAll();

        // 将所有记录传递给要返回的jsp页面，放在userList当中
        modelMap.addAttribute("userList", userList);

        // 返回pages目录下的admin/users.jsp页面
        return "admin/users";
    }

    /**
     * 添加学生 页面
     *
     * @return
     */
    @RequestMapping(value = "/admin/users/add", method = RequestMethod.GET)
    public String addUser() {
        // 转到 admin/addUser.jsp页面

        return "admin/addUser";
    }

    // post请求，处理添加学生请求，并重定向到学生管理页面
    @RequestMapping(value = "/admin/users/addP", method = RequestMethod.POST)
    public String addUserPost(@ModelAttribute("user") StudentEntity userEntity) {
        // 注意此处，post请求传递过来的是一个UserEntity对象，里面包含了该学生的信息
        // 通过@ModelAttribute()注解可以获取传递过来的'user'，并创建这个对象

        // 数据库中添加一个学生，该步暂时不会刷新缓存
        //userRepository.save(userEntity);


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        try {
            Date d = df.parse(df.format(new Date()));
            userEntity.setCreateDate(d);
        } catch (Exception ex) {
            userEntity.setCreateDate(new Date(1970, 01, 01));
        }

        // 数据库中添加一个学生，并立即刷新缓存
        studentRepository.saveAndFlush(userEntity);

        // 重定向到学生管理页面，方法为 redirect:url
        return "redirect:/admin/users";
    }

    // 查看学生详情
    // @PathVariable可以收集url中的变量，需匹配的变量用{}括起来
    // 例如：访问 localhost:8080/admin/users/show/1 ，将匹配 id = 1
    @RequestMapping(value = "/admin/users/show/{id}", method = RequestMethod.GET)
    public String showUser(@PathVariable("id") Integer userId, ModelMap modelMap) {

        // 找到userId所表示的学生
        StudentEntity userEntity = studentRepository.findOne(userId);

        // 传递给请求页面
        modelMap.addAttribute("user", userEntity);
        return "admin/userDetail";
    }

    // 更新学生信息 页面
    @RequestMapping(value = "/admin/users/update/{id}", method = RequestMethod.GET)
    public String updateUser(@PathVariable("id") Integer userId, ModelMap modelMap) {

        // 找到userId所表示的学生
        StudentEntity userEntity = studentRepository.findOne(userId);

        // 传递给请求页面
        modelMap.addAttribute("user", userEntity);

        return "admin/updateUser";
    }

    // 更新学生信息 操作
    @RequestMapping(value = "/admin/users/updateP", method = RequestMethod.POST)
    public String updateUserPost(@ModelAttribute("userP") StudentEntity user) {

        // 更新学生信息
        studentRepository.updateUser(user.getUsername(), user.getPassword(), user.getId());
        studentRepository.flush(); // 刷新缓冲区

        return "redirect:/admin/users";
    }

    // 删除学生
    @RequestMapping(value = "/admin/users/delete/{id}", method = RequestMethod.GET)
    public String deleteUser(@PathVariable("id") Integer userId) {

        // 删除id为userId的学生
        studentRepository.delete(userId);
        // 立即刷新
        studentRepository.flush();
        return "redirect:/admin/users";
    }


    @RequestMapping(value = "/admin/users/search", method = RequestMethod.GET)
    public String findUser() {

        return "admin/search";
    }

    //搜索
    @RequestMapping(value = "/admin/users/search", method = RequestMethod.POST)
    public String findUser(String uname, ModelMap modelMap) {

        if (uname != null && uname != "") {

            StudentEntity entity = studentRepository.searchWithLike(uname);

            modelMap.addAttribute("result", entity);
        } else {
            modelMap.addAttribute("result", null);
        }



        return "admin/search";
    }

}
